package com.example.demo.controller;

import com.example.demo.Dao.DaoFactory;
import com.example.demo.bean.Goods;
import com.example.demo.bean.Goods_temp;
import com.example.demo.service.GoodsDao;
import com.example.demo.service.GoodsDao_temp;
import com.example.demo.service.UserDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

@RestController
@CrossOrigin
@RequestMapping("/goodstemp")
public class Good_tempController {
    @Autowired
    GoodsDao dao = DaoFactory.getGoodsDao();
    @Autowired
    GoodsDao_temp dao_temp = DaoFactory.getGoodsDao_temp();


    @RequestMapping("/showgoods")
    public List<Goods_temp> showgoods(HttpServletRequest request, HttpServletResponse response , Model m) throws Exception {
        long orderid = Integer.parseInt(request.getParameter("orderid"));
        List<Goods_temp> list_temp=dao_temp.findGoodsByOrderId(orderid);
        return list_temp;
    }

//    @RequestMapping("/updategoods")
//    public boolean updategoods(HttpServletRequest request, HttpServletResponse response , Model m) throws Exception {
//        long id_update = Integer.parseInt(request.getParameter("id"));
//        List<Goods> list= dao.findGoodsById(id_update);
//        if(!dao.checkId(id_update)){
//            return false;
//        }
//        Goods goods;
//        Goods_temp goods_temp=new Goods_temp();
//        goods=list.get(0);
//        if(dao_temp.checkId(id_update)){
//            List<Goods_temp> list_temp= dao_temp.findGoodsById(id_update);
//            goods_temp=list_temp.get(0);
//            int n=goods_temp.getAmount();
//            n=n+1;
//            goods_temp.setAmount(n);
//            dao_temp.update(goods_temp);
//        }
//        else{
//            goods_temp.setId(goods.getId());
//            goods_temp.setName(goods.getName());
//            goods_temp.setAmount(1);
//            goods_temp.setPrice(goods.getPrice());
//            dao_temp.add(goods_temp);
//        }
//
//        return true;
//    }

    @RequestMapping("/deletegoods")
    public boolean delete(HttpServletRequest request, HttpServletResponse response ,Model m) throws Exception {
        long id_delete = Integer.parseInt(request.getParameter("id"));
        System.out.println(id_delete);
        dao_temp.del(id_delete);
        return true;
    }

    @RequestMapping("/clear")
    public boolean clear(HttpServletRequest request, HttpServletResponse response ,Model m) throws Exception {
        dao_temp.clear();
        return true;
    }

}
